package com.tcc.mynote;

/* 第二个页面     主要是周六日的行程安排 */

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.tcc.datebase.MyDB;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Fragment2 extends Fragment {

    private View view;
    private Intent intent;
    private ListView weekendList;
    private Button sta_add;
    private Button sun_add;
    private String weekend_title;

    private DatePicker datePicker;

    private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG_ID = 0;


    //初始化界面
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab_2, null);
        //周末列表
        weekendList = (ListView) view.findViewById(R.id.weekendlist);
        weekendList.setVisibility(ListView.VISIBLE);

        sta_add = (Button) view.findViewById(R.id.sta_add);
        sun_add = (Button) view.findViewById(R.id.sun_add);
        sta_add.setOnClickListener(new MyButtonListener());
        sun_add.setOnClickListener(new MyButtonListener());
        //初始化周末课表
        initWeekend();

        return view;
    }


    // 初始化周末列表
    public void initWeekend() {
        ArrayList<Map<String, Object>> listItem = new ArrayList<Map<String, Object>>();
        HashMap<String, Object> item = new HashMap<String, Object>();
        Cursor c = new MyDB(this.getActivity()).queryWeekendCourses();
        String week = null;
        String whichLesson = null;
        String cName = null;
        String period = null;
        String startTime = null;
        String endTime = null;
        String address = null;
        String remindTime = null;
        String isRemind = null;
        String remind = null;
        String isRemindByVibrato = null;
        String isRemindByRing = null;
        String teacher = null;
        int i = 0;
        int count = c.getCount();
        int weedId[] = new int[count];

        if (c.getCount() > 0) {
            while (c.moveToNext()) {
                weedId[i] = c.getInt(c.getColumnIndex("id"));
                week = c.getString(c.getColumnIndex("week"));
                whichLesson = c.getString(c.getColumnIndex("whichLesson"));
                cName = c.getString(c.getColumnIndex("cName"));
                if (cName.equals("有课")) {
                    cName = "";
                }
                period = c.getString(c.getColumnIndex("period"));
                startTime = c.getString(c.getColumnIndex("startTime"));
                endTime = c.getString(c.getColumnIndex("endTime"));
                address = c.getString(c.getColumnIndex("address"));
                remindTime = c.getString(c.getColumnIndex("remindTime"));
                isRemind = c.getString(c.getColumnIndex("isRemind"));
                isRemindByVibrato = c.getString(c.getColumnIndex("isRemindByVibrato"));
                isRemindByRing = c.getString(c.getColumnIndex("isRemindByRing"));

                if (period.equals("0")) {
                    period = "单双周";
                } else if (period.equals("11") || period.equals("12")) {
                    period = "单周";
                } else if (period.equals("21") || period.equals("22")) {
                    period = "双周";
                }
                if (isRemind.equals("1") && isRemindByVibrato.equals("1") && isRemindByRing.equals("1")) {
                    remind = "提醒开启（" + remindTime + "）：震动+铃声";
                }
                if (isRemind.equals("1") && isRemindByVibrato.equals("1") && isRemindByRing.equals("0")) {
                    remind = "提醒开启（" + remindTime + "）：震动";
                }
                if (isRemind.equals("1") && isRemindByVibrato.equals("0") && isRemindByRing.equals("1")) {
                    remind = "提醒开启（" + remindTime + "）：铃声";
                }
                if (isRemind.equals("1") && isRemindByVibrato.equals("0") && isRemindByRing.equals("0")) {
                    remind = "提醒关闭（" + remindTime + "）";
                }
                if (isRemind.equals("0")) {
                    remind = "提醒关闭（" + remindTime + "）";
                }
                teacher = c.getString(c.getColumnIndex("teacher"));

                item = new HashMap<String, Object>();
                item.put("星期", "星期" + week + "  " + whichLesson + "节");
                item.put("细节", period + " " + cName + " " + address + " " + startTime + "-" + endTime + " \n" + remind + " " + teacher);
                listItem.add(item);
                i++;

            }

        }
        SimpleAdapter adapter = new SimpleAdapter(this.getActivity(), listItem,
                android.R.layout.simple_list_item_2, new String[]{"星期", "细节"},
                new int[]{android.R.id.text1, android.R.id.text2});

        weekendList.setAdapter(adapter);
        weekendList.setVisibility(ListView.VISIBLE);
        weekendList.setOnItemClickListener(new WeekendOnItemClickListener(weedId, week, whichLesson));
        c.close();
    }

    // 周末列表事件内部类
    class WeekendOnItemClickListener implements OnItemClickListener {

        int[] wid;
        String week;
        String whichLesson;

        public WeekendOnItemClickListener(int[] wid, String week, String whichLesson) {
            // TODO Auto-generated constructor stub
            this.wid = wid;
            this.week = week;
            this.whichLesson = whichLesson;

        }

        public void onItemClick(AdapterView<?> parent, View view, final int position,
                                long id) {
            // TODO Auto-generated method stub
            String infoItemp[] = {"编辑", "删除"};
            AlertDialog.Builder builder = new Builder(getActivity());
            builder.setTitle("操作菜单").setItems(infoItemp, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    switch (which) {
                        case 0:
                            getActivity().finish();
                            intent = getActivity().getIntent();
                            intent.setClass(getActivity(), CourseInfo.class);
                            intent.putExtra("id", wid[position] + "");
                            getActivity().startActivity(intent);
                            break;
                        case 1:
                            new MyDB(getActivity()).deleteCourseById(wid[position]);// 删除数据库里的对应记录
//						System.out.println("delete:"+wid[position]);

                            // 更新显示
//						initTable();
                            initWeekend();
//						initPlan();
                            break;

                        default:
                            break;
                    }
                }
            })
                    .setPositiveButton("取消", null).show();
        }
    }

    // 按钮事件内部类MyButtonListener
    class MyButtonListener implements OnClickListener {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            switch (v.getId()) {
                case R.id.sta_add:
                    openWeekendDialog(sta_add.getText().toString());
                    break;
                case R.id.sun_add:
                    openWeekendDialog(sun_add.getText().toString());
                    break;
            }
        }

        public void openWeekendDialog(final String weekend) {

            if (weekend.equals("星期六")) {
                weekend_title = "六" + "  ";
            } else if (weekend.equals("星期日")) {
                weekend_title = "日" + "  ";
            }
            final String infoItemp[] = {"1-2", "3-4", "5-6", "7-8", "9-10", "11-12"};
            AlertDialog.Builder builder = new Builder(getActivity());
            builder.setTitle("选择" + weekend + "的课次").setItems(infoItemp, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    String id = "";
                    MyDB db = new MyDB(getActivity());
                    Cursor c = null;
                    int weekId;
                    switch (which) {
                        case 0:
                            c = db.queryWeekendCourseByWeekAndWhichLesson(weekend.substring(2), infoItemp[which]);
                            if (c.getCount() > 0) {
                                while (c.moveToNext()) {
                                    weekId = c.getInt(c.getColumnIndex("id"));
                                    id = weekId + "";
                                }
                            }
                            weekend_title = weekend_title + infoItemp[which];
                            break;
                        case 1:
                            c = db.queryWeekendCourseByWeekAndWhichLesson(weekend.substring(2), infoItemp[which]);
                            if (c.getCount() > 0) {
                                while (c.moveToNext()) {
                                    weekId = c.getInt(c.getColumnIndex("id"));
                                    id = weekId + "";
                                }
                            }
                            weekend_title = weekend_title + infoItemp[which];
                            break;
                        case 2:
                            c = db.queryWeekendCourseByWeekAndWhichLesson(weekend.substring(2), infoItemp[which]);
                            if (c.getCount() > 0) {
                                while (c.moveToNext()) {
                                    weekId = c.getInt(c.getColumnIndex("id"));
                                    id = weekId + "";
                                }
                            }
                            weekend_title = weekend_title + infoItemp[which];
                            break;
                        case 3:
                            c = db.queryWeekendCourseByWeekAndWhichLesson(weekend.substring(2), infoItemp[which]);
                            if (c.getCount() > 0) {
                                while (c.moveToNext()) {
                                    weekId = c.getInt(c.getColumnIndex("id"));
                                    id = weekId + "";
                                }
                            }
                            weekend_title = weekend_title + infoItemp[which];
                            break;
                        case 4:
                            c = db.queryWeekendCourseByWeekAndWhichLesson(weekend.substring(2), infoItemp[which]);
                            if (c.getCount() > 0) {
                                while (c.moveToNext()) {
                                    weekId = c.getInt(c.getColumnIndex("id"));
                                    id = weekId + "";
                                }
                            }
                            weekend_title = weekend_title + infoItemp[which];
                            break;
                        case 5:
                            c = db.queryWeekendCourseByWeekAndWhichLesson(weekend.substring(2), infoItemp[which]);
                            if (c.getCount() > 0) {
                                while (c.moveToNext()) {
                                    weekId = c.getInt(c.getColumnIndex("id"));
                                    id = weekId + "";
                                }
                            }
                            weekend_title = weekend_title + infoItemp[which];
                            break;
                        default:
                            break;
                    }

                    c.close();
                    intent = getActivity().getIntent();
                    intent.setClass(getActivity(), CourseInfo.class);
                    intent.putExtra("id", id);
                    intent.putExtra("week_classnumber", weekend_title);
                    intent.putExtra("courseName", "");
                    intent.putExtra("address", "");
                    intent.putExtra("period", "单双周");
                    getActivity().startActivity(intent);
                    getActivity().finish();
                }
            }).setPositiveButton("取消", null).show();
        }
    }
}





